<?php

namespace app\common\model\xiluedu;

use app\common\library\Auth;
use think\Db;
use think\Exception;
use think\Log;
use think\Model;
use function fast\array_get;

class FooterView extends Model
{
    protected $name = 'xiluedu_footer_view';
    protected $autoWriteTimestamp = 'int';


    protected $createTime = 'createtime';
    protected $updateTime = false;

    const TYPE_COURSE = 1;
    const TYPE_OFFLINE = 2;

    /**
     * 查看课程
     * @param int $userId
     * @param int $footer_id
     * @return
     */
    public static function viewFooter($userId, $footer_id,$type) {
        if(!$userId || !$footer_id || !$type) {
            throw new Exception(__('invalid arguments'));
        }
        Db::startTrans();
        try {
            $record = static::where('user_id',$userId)->where('footer_id', $footer_id)->where('type',$type)->find();
            $now = time();
            if(!$record) {
                $record = new static();
                $record->save(['type'=>$type,'user_id'=>$userId,'footer_id'=>$footer_id,'view_time'=>$now]);
            }else {
                //两小时内不重复加
                $data = ['view_time'=>time()];
                if($now>$record->view_time+3600*2) {
                    $data['view_count'] = Db::raw('view_count+1');
                }
                $record->save($data);
            }
            Db::commit();
            return true;
        }catch (\Exception $e) {
            Log::error($e->getMessage()."\n".$e->getTraceAsString());
            throw $e;
        }
    }

    public function course(){
        return $this->belongsTo(Course::class,'footer_id','id',[],'INNER')->setEagerlyType(1);
    }

    public function offline(){
        return $this->belongsTo(OfflineCourse::class,'footer_id','id',[],'INNER')->setEagerlyType(1);
    }

    /**
     * 根据条件获取已领取数量
     */
    public static function getViewCount($where){
        return self::where($where)->count();
    }

    /**
     * 我的足迹
     */
    public static function lists($parmas){
        $auth = Auth::instance();
        //$date = array_get($parmas,'date',date('Y-m-d'));
        $pagesize = array_get($parmas,'pagesize',10);
        $tab = array_get($parmas,'tab');
        if($tab == 0){
            $where['type'] = static::TYPE_COURSE;
        }else{
            $where['type'] = static::TYPE_OFFLINE;
        }
        return static::where('user_id',$auth->id)
            ->where($where)
            //->where('view_time','between',[strtotime($date.' 00:00:00'),strtotime($date.' 23:59:59')])
            ->order('view_time','desc')
            ->paginate($pagesize)->each(function ($row){
                if($row['type'] == self::TYPE_COURSE){
                    $row->course;
                    //$row['data'] = $row->course ?? $row->course->toArray();
                    //unset($row->course);
                }else if($row['type'] == self::TYPE_OFFLINE){
                    $row->offline;
                    //$row['data'] = $row->offline ?? $row->offline->toArray();
                    //unset($row->offline);
                }
            });
    }

}
